<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/5/4
 * Time: 上午9:44
 */
/**
 * @Author:      lyt
 * @DateTime:  2017-05-17 15:43:39
 * @Description: 废弃接口 pending_order pay_order_by_cash  pay_order_by_credit 本版本注释掉 下个版本删除相关代码
 */ 
class Account extends Eloquent
{
    // //订单结算
    // public static function pending_order($order_id){
    //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
    //     do {
    //         //获取订单信息
    //         $order = Orders::where('order_id',$order_id)->first();
    //         //获取客户类型
    //         $user = Users::where('id',$order->user_id)->where('status',1)->first();

    //         //个人用户不能挂单
    //         if($user->type == 1 && $order->pay_type == 6){
    //             $response = array(
    //                 'code' => -1,
    //                 'msg' => 'error',
    //                 'result' => '个人用户不能挂单'
    //             );
    //             break;
    //         }

    //         //现金和刷卡支付记账
    //         if($order->pay_type == 1 || $order->pay_type == 7){
    //             if(Account::pay_order_by_cash($order_id)){
    //                 $response = array(
    //                     'code' => 0,
    //                     'msg' => 'ok',
    //                     'result' => array(
    //                         'order_id' => $order_id
    //                     )
    //                 );
    //             }else{
    //                 $response = array(
    //                     'code' => -1,
    //                     'msg' => 'error',
    //                     'result' => '支付失败'
    //                 );
    //             }
    //             break;
    //         }

    //         //个人用户余额支付
    //         if($user->type == 1 && $order->pay_type == 8){
    //             $account = UserAccount::where('user_id',$order->user_id)->first();
    //            // if(($account->balance + $account->credit) > 0){
    //                 if(Account::pay_order_by_credit($order_id)){
    //                     $response = array(
    //                         'code' => 0,
    //                         'msg' => 'ok',
    //                         'result' => array(
    //                             'order_id' => $order_id
    //                         )
    //                     );
    //                 }else{
    //                     $response = array(
    //                         'code' => -1,
    //                         'msg' => 'error',
    //                         'result' => '支付失败'
    //                     );
    //                 }
    //             // }else{
    //             //     $response = array(
    //             //         'code' => -1,
    //             //         'msg' => 'error',
    //             //         'result' => '用户额度不足'
    //             //     );
    //             // }
    //             break;
    //         }

    //         //企业客户挂账记账
    //         if($user->type == 2 && $order->pay_type == 6){
    //             $account = UserAccount::where('user_id',$order->user_id)->first();
    //             if(($account->balance + $account->credit) > 0){
    //                 if(Account::pay_order_by_credit($order_id)){
    //                     $response = array(
    //                         'code' => 0,
    //                         'msg' => 'ok',
    //                         'result' => array(
    //                             'order_id' => $order_id
    //                         )
    //                     );
    //                 }else{
    //                     $response = array(
    //                         'code' => -1,
    //                         'msg' => 'error',
    //                         'result' => '支付失败'
    //                     );
    //                 }
    //             }else{
    //                 $response = array(
    //                     'code' => -1,
    //                     'msg' => 'error',
    //                     'result' => '用户额度不足'
    //                 );
    //             }
    //             break;
    //         }


    //     } while (false);
    //     return $response;
    // }
    
    //现金结算订单
    /**
     * @Author:      lyt
     * @DateTime:    2016-05-24 17:54:18
     * @Description: 接口废弃
     */
    // public static function pay_order_by_cash($order_id){
    //     try{
    //         $exception = DB::transaction(function() use ($order_id){
    //             //获取订单信息
    //             $order = Orders::where('order_id',$order_id)->first();
    //             //todo 优惠券账务处理方式

    //             //获取订单实际支付金额
    //             $order_amount = $order->payable_amount;
    //             $tran_no = date('Ymd',time()).Common::generateCode();

    //             //记录用户充值
    //             Account::record($order->user_id,$tran_no,1,'01',$order_amount,'订单结算充值');
    //             //记录用户支出
    //             Account::record($order->user_id,$tran_no,2,'02',$order_amount,'订单结算支出');
    //             //记录公司账户收入
    //             Account::record_base('1',$tran_no,1,'01',$order_amount,'订单结算收入');
    //             //记录订单账务关系
    //             OrderAccount::insert(
    //                 array(
    //                     'order_id' => $order->order_id,
    //                     'tran_no' => $tran_no,
    //                 )
    //             );
    //         });
    //         return is_null($exception) ? true : $exception;
    //     } catch (Exception $e) {
    //         Log::error("error", array("system" => "Account" ,"event" => "shop_pending_order" ,"exception" => $e));
    //         return false;
    //     }
    // }

    //挂账结算订单
    /**
     * @Author:      lyt
     * @DateTime:    2016-05-24 17:54:00
     * @Description: 接口废弃
     */
    // public static function pay_order_by_credit($order_id){
    //     try{
    //         $exception = DB::transaction(function() use ($order_id){
    //             //获取订单信息
    //             $order = Orders::where('order_id',$order_id)->first();
    //             //todo 优惠券账务处理方式

    //             //获取订单实际支付金额
    //             $order_amount = $order->payable_amount;
    //             $tran_no = date('Ymd',time()).Common::generateCode();

    //             //记录用户支出
    //             Account::record($order->user_id,$tran_no,2,'02',$order_amount,'订单结算支出');
    //             //记录公司账户收入
    //             Account::record_base('1',$tran_no,1,'01',$order_amount,'订单结算收入');
    //             //记录订单账务关系
    //             OrderAccount::insert(
    //                 array(
    //                     'order_id' => $order->order_id,
    //                     'tran_no' => $tran_no,
    //                 )
    //             );
    //         });
    //         return is_null($exception) ? true : $exception;
    //     } catch (Exception $e) {
    //         Log::error("error", array("system" => "Account" ,"event" => "shop_pending_order" ,"exception" => $e));
    //         return false;
    //     }
    // }

    //客服调整额度
    /**
     * @Author:      lyt
     * @DateTime:  2017-05-17 15:47:41
     * @Description: 修改额度调整为 新的额度调整 
     */
    
    public static function custom_service_credit($user_id,$amount){
        try{
            $exception = DB::transaction(function() use ($user_id,$amount){
                //获取账户
                $account_no = Account::get_account_no($user_id);

                UserAccount::where('user_id',$user_id)
                    ->update(
                        array(
                            'credit' => $amount,
                        )
                    );
                //获取该用户的 挂账账户
                $de_account_no = Account::get_account_details_no($user_id,0,3);
                UserAccountDeatails::where('account_no',$de_account_no)
                    ->update(
                        array(
                            'credit' => $amount,
                        )
                    );
            });
            return is_null($exception) ? true : $exception;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "custom_service_credit" ,"exception" => $e));
            return false;
        }
    }


    //客服充值
    public static function custom_service_recharge($user_id,$amount){
        try{
            $exception = DB::transaction(function() use ($user_id,$amount){
                //交易流水
                // $tran_no = date('Ymd',time()).Common::generateCode();
                $tran_no = date('YmdHis',time()).Common::generateCode(8);

                //记录用户充值
                Account::record($user_id,$tran_no,1,'01',$amount,'客服后台充值');
            });
            return is_null($exception) ? true : $exception;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "custom_service_recharge" ,"exception" => $e));
            return false;
        }
    }


    /**
     * @Author:      lyt
     * @DateTime:  2017-05-17 15:50:54
     * @Description: 充值方法  只适用 制定类型充值 
     * @param user_id 用户ID
     * @param pay_tepe 支付类型
     *   "1"=>"线下支付现金",
     *   "7"=>"线下支付刷卡",
     *   '11'=>'线下支票',
     *   '12'=>'划拨',
     *   '13'=>'保险',
     *   '14'=>'调整充值',
     * @param acount_type 入账类型  1 现金账户，2 虚拟账户
     * @param amount 入账金额 接受单位为分
     * @param create_type 入账人身份  sc 前台  custom 用户 service cms用户 
     * @param create_by 入账人ID
     * @param shop_id 入账门店 平台填0
     * 逻辑 更改 用户总账户 和各个分账户 该方法不验证数据合法性 
     *
     */
    public static function service_recharge($user_id,$pay_type,$acount_type,$amount,$create_type,$create_by,$shop_id,$note=""){
        try{
            $exception = DB::transaction(function() use ($user_id,$pay_type,$acount_type,$amount,$create_type,$create_by,$shop_id,$note){
                //交易流水
                // $tran_no = date('Ymd',time()).Common::generateCode();
                $tran_no = date('YmdHis',time()).Common::generateCode(8);
                $aur=new AcountUserRecharge();
                $aur->user_id=$user_id;
                $aur->account_no="";
                $aur->pay_type=$pay_type;
                $aur->current_amount=$amount;
                $aur->amount=$amount;
                $aur->status=1;
                $aur->create_type=$create_type;
                $aur->create_by=$create_by;
                $aur->tran_no=$tran_no;
                $aur->shop_id=$shop_id;
                $aur->implement_time=Common::currentTimeString();
                $aur->save();
                switch ($pay_type) {
                    case 1:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,1);
                        Account::record($user_id,$shop_id,1,$tran_no,1,108,$amount,'现金充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 2:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,1);
                        Account::record($user_id,$shop_id,1,$tran_no,1,129,$amount,'支付宝充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 3:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,1);
                        Account::record($user_id,$shop_id,1,$tran_no,1,128,$amount,'微信充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 7:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,1);
                        Account::record($user_id,$shop_id,1,$tran_no,1,109,$amount,'刷卡充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 11:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,1);
                        Account::record($user_id,$shop_id,1,$tran_no,1,110,$amount,'支票充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 12:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,$acount_type);
                        Account::record($user_id,$shop_id,$acount_type,$tran_no,1,111,$amount,'划拨充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 13:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,2);
                        Account::record($user_id,$shop_id,2,$tran_no,1,112,$amount,'保险充值收入,充值记录:'.$aur->id.$note);
                        break;
                    case 14:
                        $account_no = Account::get_account_details_no($user_id,$shop_id,$acount_type);
                        Account::record($user_id,$shop_id,$acount_type,$tran_no,1,113,$amount,'调整充值收入,充值记录:'.$aur->id.$note);
                        break;
                }
                AcountUserRecharge::where("id",$aur->id)->update(array("account_no"=>$account_no));
                return $aur->id;
            });
            return is_null($exception) ? true : $exception;
        } catch (Exception $e) {
            Log::error("error", array("system" => "Account" ,"event" => "service_recharge" ,"exception" => $e));
            return false;
        }
    }

    //生成市场活动优惠券
    //1.公司账户预授权到市场活动账号
    //2.每次使用一张优惠券结算,市场活动账号支出优惠券金额,公司主账户收入优惠券金额

    public static function record_base($account_no,$tran_no,$action,$subject,$amount,$note = ''){
        //记录账务流水
        $journal_account = new JournalAccount();
        $journal_account->account_no = $account_no;
        $journal_account->tran_no = $tran_no;
        $journal_account->action = $action;
        $journal_account->subject = $subject;
        $journal_account->amount = $amount;
        $journal_account->note = $note;
        $journal_account->save();

        //更新总账
        // $user_account = BaseAccount::where('account_no',$account_no)->first();
        if($action == 1){ //存入
            // BaseAccount::where('account_no',$account_no)
            //     ->update(
            //         array(
            //             'balance' => $user_account->balance + $amount,
            //         )
            //     );
            DB::update("update base_account set balance=balance+? where account_no=?",array($amount,$account_no));
        }else{ //支出
            // BaseAccount::where('account_no',$account_no)
            //     ->update(
            //         array(
            //             'balance' => $user_account->balance - $amount,
            //         )
            //     );
            DB::update("update base_account set balance=balance-? where account_no=?",array($amount,$account_no));
        }
    }
    /**
     * @Author:      lyt
     * @DateTime:  2017-05-15 15:51:33
     * @Description: 
     * add  shop_id 参数 用于用户的支付门店
     * add  acount_type 参数 用于用户的支类型
     */ 
    public static function record($user_id,$shop_id,$acount_type,$tran_no,$action,$subject,$amount,$note = ''){
        //获取账户
        $account_no = Account::get_account_no($user_id);     
        //更新总账
        // $user_account = UserAccount::where('account_no',$account_no)->first();
        if($action == 1){ //存入
            // UserAccount::where('account_no',$account_no)
            //     ->update(
            //         array(
            //             'balance' => $user_account->balance + $amount,
            //         )
            //     );
            DB::update("update user_account set balance=balance+? where account_no=?",array($amount,$account_no));
        }else{ //支出
            // UserAccount::where('account_no',$account_no)
            //     ->update(
            //         array(
            //             'balance' => $user_account->balance - $amount,
            //         )
            //     );
            DB::update("update user_account set balance=balance-? where account_no=?",array($amount,$account_no));
        }
        //获取分账户
        $account_no = Account::get_account_details_no($user_id,$shop_id,$acount_type);
        //记录账务流水
        $journal_account = new JournalAccount();
        $journal_account->account_no = $account_no;
        $journal_account->tran_no = $tran_no;
        $journal_account->action = $action;
        $journal_account->subject = $subject;
        $journal_account->amount = $amount;
        $journal_account->note = $note;
        $journal_account->save();
        //更新分账户
        // $user_account = UserAccountDeatails::where('account_no',$account_no)->first();
        if($action == 1){ //存入
            // UserAccountDeatails::where('account_no',$account_no)
            //     ->update(
            //         array(
            //             'balance' => $user_account->balance + $amount,
            //         )
            //     );
            DB::update("update user_account_details set balance=balance+? where account_no=?",array($amount,$account_no));
        }else{ //支出
            // UserAccountDeatails::where('account_no',$account_no)
            //     ->update(
            //         array(
            //             'balance' => $user_account->balance - $amount,
            //         )
            //     );
            DB::update("update user_account_details set balance=balance-? where account_no=?",array($amount,$account_no));
        }
    }

    public static function get_account_no($user_id){
        $account = UserAccount::where('user_id',$user_id)->first();
        if(is_null($account)){ //不存在则新建
            $user = Users::where('id',$user_id)->first();

            $account_no = date('Ymd',time()).'0000'.sprintf("%06d", $user->id);
            $user_account = new UserAccount();
            $user_account->account_no = $account_no;
            $user_account->user_id = $user_id;
            $user_account->type = $user->type;
            $user_account->balance = 0;
            $user_account->credit = 0;
            $user_account->save();
        }else{ //存在则直接返回
            $account_no = $account->account_no;
        }
        return $account_no;
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-05-15 16:29:48
     * @Description: 添加或去用户账户详情的 方法 主要账务信息 记录在 这个表中
     */
    public static function get_account_details_no($user_id,$shop_id=0,$acount_type=1){
        $account = UserAccountDeatails::where('user_id',$user_id)->where('shop_id',$shop_id)->where('acount_type',$acount_type)->first();
        if(is_null($account)){ //不存在则新建
            $user = Users::where('id',$user_id)->first();
            $account_no = date('Ymd',time()).'0000'.sprintf("%06d", $user->id).'0'.$acount_type.$shop_id;
            $user_account = new UserAccountDeatails();
            $user_account->account_no = $account_no;
            $user_account->user_id = $user_id;
            $user_account->type = $user->type;
            $user_account->acount_type = $acount_type;
            $user_account->shop_id = $shop_id;
            $user_account->balance = 0;
            $user_account->credit = 0;
            $user_account->save();
        }else{ //存在则直接返回
            $account_no = $account->account_no;
        }
        return $account_no;
    }
}